package cn.itsource.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.itsource.constants.Constants;
import cn.itsource.constants.ErrorCode;
import cn.itsource.pojo.app.dto.MpRegisterDto;
import cn.itsource.pojo.bo.OpenIdResult;
import cn.itsource.pojo.domain.Driver;
import cn.itsource.mapper.DriverMapper;
import cn.itsource.pojo.properties.MpProperties;
import cn.itsource.service.IDriverService;
import cn.itsource.service.IDriverSettingService;
import cn.itsource.service.IDriverSummaryService;
import cn.itsource.service.IDriverWalletService;
import cn.itsource.utils.AssertUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * <p>
 * 司机对象 服务实现类
 * </p>
 *
 * @author tom
 * @since 2024-03-14
 */
@Service
@Slf4j
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements IDriverService {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private IDriverSettingService settingService;
    @Autowired
    private IDriverWalletService walletService;
    @Autowired
    private IDriverSummaryService summaryService;
    @Autowired
    private MpProperties mpProperties;

    @Override
    public Boolean mpRegister(MpRegisterDto dto) {
        //1.使用三个参数发送请求
        //wx1943975075b07945 - fef216223b8cb7047916afb41ea12796 - code
        //地址抽常量，appid和secret抽属性
        String url = String.format(Constants.URL.URL_OPEN_ID,
                mpProperties.getAppid(),
                mpProperties.getSecret(),
                dto.getWxCode());

        //2.对结果进行处理和校验
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        //打印值
        log.info("获取微信ID结果：{} - {}",responseEntity.getStatusCode(),responseEntity.getBody());
        //校验
        AssertUtil.isEquals(responseEntity.getStatusCodeValue(), HttpStatus.OK.value(), ErrorCode.WxCODE_ERROR.getMessage());

        //3.获取Openid
        //json字符串转json对象
        OpenIdResult openIdResult = JSONObject.parseObject(responseEntity.getBody(), OpenIdResult.class);

        //4.保存司机，钱包，设置，统计表【共享主键一对一】
        LambdaQueryWrapper<Driver> query
                = new LambdaQueryWrapper<Driver>().eq(Driver::getOpenId, openIdResult.getOpenid());
        Driver driver = super.getOne(query);
        //如果通过openid找到了Driver对象 => 已经注册了直接去登录
        AssertUtil.isNull(driver,"已注册，请直接登录");

        //构建Druver对象并保存
        driver = createDriver(openIdResult.getOpenid());
        settingService.create(driver.getId());
        walletService.create(driver.getId());
        summaryService.create(driver.getId());

        return true;
    }


    private Driver createDriver(String openid) {
        //使用雪花算法生成id【1,2是随便写的】
        long driverId = IdUtil.createSnowflake(1, 2).nextId();
        Driver driver = new Driver()
                .setCreateTime(new Date())
                .setId(driverId)
                .setOpenId(openid);
        super.save(driver);
        return driver;
    }


}
